import { Request, toast, distance } from '../../utils/util.js'
let request = new Request()
const app = getApp()
Page({
  data: {
    orderId : 0,
    formData : {
      chargeType : 1,
      quantity : 10,
      deviceNo : '',
      portNo : 1
    }
  },

  input (e) {
    let target = e.currentTarget.dataset.target
    let value = e.detail.value
    let update = {}
    update['formData.' + target] = value
    this.setData(update)
  },

  scan () {
    wx.scanCode({
      success: res => {
        let params = queryParams(res.result)
        let deviceNo = params.deviceNo
        let portNo = params.portNo || 0
        this.setData({ 'formData.deviceNo': deviceNo})
        if (portNo) {
          this.setData({ 'formData.portNo': portNo })
        }
      }
    })
  },

  charge () {
    let formData = Object.assign({}, this.data.formData)
    wx.showLoading({
      title: '',
    })
    request.post('testcharge/charge', res => {
      if(res.success) {
        let orderId = res.data.orderId
        let inter = setInterval(() => {
          request.post('testcharge/chargeResult', res => {
            if(res.success) {
              let status = res.data.status
              if(status != 0) {
                wx.hideLoading()
                clearInterval(inter)
               if(status == 1) {
                 toast('充电成功')
                 this.setData({ orderId: orderId })
               }else{
                 toast('充电失败')
               }
              }
            }else{
              wx.hideLoading()
              toast(res.msg)
            }
          }, {id : orderId})
        }, 2000)
      }else{
        wx.hideLoading()
        toast(res.msg)
      }
    }, formData)
  },

  addCharge () {
    let formData = Object.assign({}, this.data.formData)
    formData.id = this.data.orderId
    wx.showLoading({
      title: '',
    })

    request.post('testcharge/extendCharge', res => {
      if (res.success) {
        let orderId = res.data.orderId
        let inter = setInterval(() => {
          request.post('testcharge/extendChargeResult', res => {
            if (res.success) {
              let status = res.data.status
              if (status != 0) {
                wx.hideLoading()
                clearInterval(inter)
                if (status == 1) {
                  toast('增加充电成功')
                  this.setData({ orderId: 0 })
                } else {
                  toast('增加充电失败')
                }
              }
            } else {
              wx.hideLoading()
              toast(res.msg)
            }
          }, { id: orderId })
        }, 2000)
      } else {
        wx.hideLoading()
        toast(res.msg)
      }
    }, formData)

  },
  
  stopCharge () {
    let formData = Object.assign({}, this.data.formData)
    formData.id = this.data.orderId
    wx.showLoading({
      title: '',
    })

    wx.showLoading({
      title: '',
    })

    request.post('testcharge/stopCharge', res => {
      if (res.success) {
        let orderId = res.data.orderId
        let inter = setInterval(() => {
          request.post('testcharge/chargeResult', res => {
            if (res.success) {
              let status = res.data.status
              if (status != 0) {
                wx.hideLoading()
                clearInterval(inter)
                toast('停止充电成功')
                this.setData({ orderId: 0 })
              }
            } else {
              wx.hideLoading()
              toast(res.msg)
            }
          }, { id: orderId })
        }, 2000)
      } else {
        wx.hideLoading()
        toast(res.msg)
      }
    }, formData)
  },

  onLoad: function (options) {
    
  }
})